import React, {Component} from 'react';
import {Table, Modal, message, Button, Icon} from "antd";
import {reqPublicResume, reqCostPoint, reqPublicToPrivate, reqIsOK, reqIsAllOK} from '../../api/index'
import memoryUtils from "../../utils/memoryUtils";
import {PAGE_SIZE} from '../../utils/constants';
import LinkButton from "../../components/link-button/index";
import Search from '../../components/search/index'
import './resume-public.less'

const {confirm} = Modal;
/*
* 求职者信息路由*/
export default class Public extends Component {
    state = {
        selectedRowKeys: [], // Check here to configure the default column
        size: 'default',
        bordered: true,
        searchText: '',
        resume: [],//当前页面简历列表
        loading: false,//是否正在加载中
        count: 0,
        keyword: '',
        visiable: false,
    };

    //初始化Table所有列的数组
    initColumns = () => {
        this.columns = [
            {
                title: '姓名',
                dataIndex: 'row1',
                key: 'row1',
                width: 100,
                render: (row1) => {
                    return <div>{row1.name}<br/>{row1.gender}-{row1.age}-{row1.education}</div>
                },
            },
            {
                title: '求职期望',
                dataIndex: 'row2',
                key: 'row2',
                width: 300,
                render: (row2) => {
                    return <div>期望职位:{row2.exp_postion}<br/>期望行业:{row2.exp_trade}<br/>期望薪资:{row2.exp_salary}</div>
                },
            },
            {
                title: '过往履历',
                dataIndex: 'row3',
                key: 'row3',
                width: 300,
                render: (row3) => {
                    return <div>从事职位:{row3.now_postion}<br/>从事行业:{row3.now_trade}<br/>从事时间:{row3.worktime}</div>
                },
            },
            {
                title: '目前状态',
                dataIndex: 'row4',
                key: 'row4',
                width: 300,
                render: (row4) => {
                    return <div>求职状态:{row4.status}<br/>排斥行业:{row4.rej_trade}<br/>排斥工作:{row4.rej_postion}</div>
                },
            },
            {
                title: '住址信息',
                dataIndex: 'row5',
                key: 'row5',
                width: 200,
                render: (row5) => {
                    return <div>住址:{row5.location}<br/>籍贯:{row5.city}</div>
                },
            },
            {
                title: '操作',
                key: 'operation',
                width: 100,
                render: (results) => <span
                    onClick={() => this.showDeleteConfirm(results)}><LinkButton>加入私有库</LinkButton></span>,
            },
        ]
    }

    initData = async (page) => {
        const keyword = this.state.keyword
        this.setState({loading: true})//显示loading
        const result = await reqPublicResume(keyword, page, PAGE_SIZE)
        console.log(result)
        this.setState({loading: false})//隐藏
        const count = result.data.count
        const data = []
        if (result.status === 200) {
            //请求成功
            const resume = result.data.results
            resume.map(item => data.push({
                row1: {
                    name: item.name,
                    gender: item.gender,
                    age: item.age,
                    education: item.education,
                },
                row2:{
                    exp_postion:item.exp_postion,
                    exp_trade: item.exp_trade,
                    exp_salary: item.exp_salary,
                },
                row3:{
                    now_postion: item.now_postion,
                    now_trade:item.now_trade,
                    worktime: item.worktime,
                },
                row4:{
                    status:item.status,
                    rej_trade: item.rej_trade,
                    rej_postion: item.rej_postion,
                },
                row5:{
                    location:item.location,
                    city: item.city,
                },
                key: item.id,
                employee: item.employee,
            }))
        }
        this.setState({
            resume: data,
            count: count,
        })
    }

    start = async () => {
        this.setState({loading: true});
        const selectedRowKeys = this.state.selectedRowKeys
        const employees = selectedRowKeys
        const result = await reqIsAllOK(employees)
        console.log("在不在")
        console.log(result)
        message.success('已操作，查看私有库！')
        // if(result.data.msg==='成功'){
        //     message.success('')
        // }
        // ajax request after empty completing
        setTimeout(() => {
            this.setState({
                selectedRowKeys: [],
                loading: false,
            });
        }, 1000);
    };

    onSelectChange = selectedRowKeys => {
        this.setState({selectedRowKeys});
    };

    showDeleteConfirm(results) {
        memoryUtils.employee = results.employee
        confirm({
            title: '确定要将该简历加入私有库?',
            content: '加入私有库将消耗1积分',
            okText: 'Yes',
            okType: 'danger',
            cancelText: 'No',
            onOk: () => {
                this.isOK()
            },
            onCancel() {
            },
        });
    }


    isOK = async () => {
        const point = 1
        const employee = memoryUtils.employee
        const result = await reqIsOK(employee, point)
        if (result.data.msg === '可以加入私有库') {
            this.costPoint()
        } else if (result.data.msg === '积分不足') {
            message.warning('积分不足，请先充值!！')
        } else if (result.data.msg === '已加入私有库') {
            message.warning('私有库已存在')
        } else if (result.data.msg === '无对应用户所在公司或者求职者id') {
            message.error('无对应用户所在公司或者求职者id')
        } else {
            message.error('用户未登录，请立即登陆')
        }
    }


    //公有库简历单个加入私有库扣积分操作
    costPoint = async () => {
        const points = 1
        const state = 'success'
        if (memoryUtils.points >= points) {
            const result = await reqCostPoint(points, state)
            this.publicToPrivate()
        }
    }

    //公有库简历单个加入私有库
    publicToPrivate = async () => {
        const employee = memoryUtils.employee
        const result = await reqPublicToPrivate(employee)
        if (result.status === 201) {
            message.success('简历加入私有库成功！')
        }
    }

    //接受子组件Search传来的值
    getSearchResult = (keyword, resume, count) => {
        console.log("见证奇迹的时刻")
        console.log(keyword, resume, count)
        this.setState({
            resume: resume,
            count: count,
            keyword: keyword,
            visiable: true,
        })
    }

    //移除搜索条件
    removeData = () => {
        console.log("到这里了")
        this.setState({
            keyword: '',
            resume: [],//当前页面简历列表
            count: 0,
            visiable: false,
        }, () => {
            this.initData()
        })
    }

    //为第一次render()准备数据
    componentWillMount() {
        this.initColumns()

    }

    //发异步ajax请求
    componentDidMount() {
        this.initData()
    }

    render() {
        //读取状态数据
        const {resume, count, loading, selectedRowKeys, keyword, visiable} = this.state;
        const rowSelection = {
            selectedRowKeys,
            onChange: this.onSelectChange,
        };
        const hasSelected = selectedRowKeys.length > 0;
        return (
            <div>
                <div className="resume-public">
                    <Button type="primary" onClick={this.start} disabled={!hasSelected} loading={loading}>
                        批量加入私有库
                    </Button>
                    <span style={{marginLeft: 8}}>
            {hasSelected ? `已选中 ${selectedRowKeys.length} 条简历` : ''}
                    </span>
                    <span style={{marginLeft: 50}}>共{count}条简历</span>
                    <div className="search-border">
                        <div className="search">
                            <Search public={this}/>
                        </div>
                        <div className="keyword">
                            关键字:
                            <span><LinkButton>{keyword}</LinkButton></span>{visiable ?
                            <Icon type="close" onClick={this.removeData}/> : <span></span>}
                        </div>
                    </div>
                </div>
                <Table {...this.state}
                       rowSelection={rowSelection}
                       columns={this.columns}
                       dataSource={resume}
                       scroll={{x: '100%', y: '100%'}}
                       loading={loading}
                       pagination={{
                           total: count,
                           defaultPageSize: PAGE_SIZE,
                           onChange: (page) => {
                               this.initData(page)
                           }
                       }}/>
            </div>
        )
    }
}
